package a66weding.com.jiehuntong.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.AddressListBean;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * 购物车地址选择
 */
public class MeUserAddressActivity extends MeBaseActivity implements View.OnClickListener, OnNetResultListener {
    private LinearLayout mBackLay;
    private TextView mBackContentTv;
    private TextView mHeaderTitleTv;
    private RelativeLayout mHeaderRightRlay;
    private Button mAddAddress;
    private Button mAddBt;
    private ListView lv;
    private ArrayList<AddressListBean.DataBean> list;
    private AddressListBean addressListBean;
    private int id;
    private LinearLayout mShowList;
    private RelativeLayout mNoList;
    private int memberId;
    private int secelt;
    private String name;
    private String phone;
    private String address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_user_shippingaddress);
        ((GlobalApplication) getApplication()).getActivityManager().addActivity("MeUserShippingaddressActivity", this);

        initview();

    }


    private void initview() {
        mBackLay = (LinearLayout) findViewById(R.id.llay_header_back);
        mBackContentTv = (TextView) findViewById(R.id.tv_header_backcontent);
        mHeaderTitleTv = (TextView) findViewById(R.id.tv_header_title);
        mHeaderRightRlay = (RelativeLayout) findViewById(R.id.rlay_header_rightfunc);
        mAddAddress = (Button) findViewById(R.id.btn_me_add);
        mAddBt = (Button) findViewById(R.id.btn_add);
        lv = (ListView) findViewById(R.id.lview_me_setaddress_list);
        mShowList = (LinearLayout) findViewById(R.id.rlay_me_setaddresscontent);
        mNoList = (RelativeLayout) findViewById(R.id.rlay_me_setaddressempty);

        mBackContentTv.setText("返回");
        mHeaderTitleTv.setText("我的收货地址");
        mHeaderRightRlay.setVisibility(View.GONE);

        mBackLay.setOnClickListener(this);
        mAddAddress.setOnClickListener(this);
        mAddBt.setOnClickListener(this);

    }

    //在页面可见的时候做网络请求
    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 页面初始化数据调用的方法
     */
    private void initData() {
        String url = UrlDealUtil.getRealUrlNewId(RequestCode.RETURN_ADDRESSISDEFAULT_1);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                this, AddressListBean.class, RequestCode.RETURN_ADDRESSISDEFAULT_1);
        HttpHandleUtil.executeCommonPost(this, httpRequestBean);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.llay_header_back: //点击返回,关闭界面
                finish();
                break;
            case R.id.btn_me_add:   //点击添加,跳转到添加界面
                intent.setClass(MeUserAddressActivity.this, MeUserAddaddressActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_add:   //点击添加,跳转到添加界面
                intent.setClass(MeUserAddressActivity.this, MeUserAddaddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof AddressListBean) {
            addressListBean = (AddressListBean) data;
            list = (ArrayList<AddressListBean.DataBean>) addressListBean.getData();
            if (list == null) {
                mShowList.setVisibility(View.GONE);
                mNoList.setVisibility(View.VISIBLE);
            } else {
                mShowList.setVisibility(View.VISIBLE);
                mNoList.setVisibility(View.GONE);
                lv.setAdapter(new MyAdapter());
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        name = list.get(position).getNvc_Consignee();
                        phone = list.get(position).getNvc_MobilePhone();
                        address = list.get(position).getPName() + list.get(position).getCName() +
                                list.get(position).getAName() + list.get(position).getNvc_StreetAddress();

                        int i_id = list.get(position).getI_Id();

                        int i_aId = list.get(position).getI_AId();
                        int i_cId = list.get(position).getI_CId();
                        int i_pId = list.get(position).getI_PId();
                        String aName = list.get(position).getAName();
                        String cName = list.get(position).getCName();

                        MyAddressEvent event = new MyAddressEvent(list.get(position));
                        EventBus.getDefault().post(event);
                        finish();
                    }
                });
            }
        } else if (data instanceof CommonResultBean) {
            onResume();
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {
        Toast.makeText(MeUserAddressActivity.this, "请求数据失败", Toast.LENGTH_SHORT).show();
    }


    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder mHolder;
            if (convertView != null) {
                view = convertView;
                mHolder = (ViewHolder) view.getTag();
            } else {
                view = View.inflate(getApplicationContext(), R.layout.item_me_show_address, null);
                mHolder = new ViewHolder();
                mHolder.mName = (TextView) view.findViewById(R.id.tv_name);
                mHolder.mPhone = (TextView) view.findViewById(R.id.tv_phone_number);
                mHolder.mAddress = (TextView) view.findViewById(R.id.tv_address);
                mHolder.mAddressIv = (ImageView) view.findViewById(R.id.iv_me_address);
                view.setTag(mHolder);
            }
            mHolder.mName.setText(list.get(position).getNvc_Consignee());
            mHolder.mPhone.setText(list.get(position).getNvc_MobilePhone());
            mHolder.mAddress.setText(list.get(position).getPName() + list.get(position).getCName() +
                    list.get(position).getAName() + list.get(position).getNvc_StreetAddress());

            secelt = list.get(position).getIsDefault();
            if (secelt != 1) {
                mHolder.mAddressIv.setVisibility(View.GONE);
            }
            return view;
        }
    }

    class ViewHolder {
        TextView mName;
        TextView mPhone;
        TextView mAddress;
        ImageView mAddressIv;
    }

    public class MyAddressEvent{

        private AddressListBean.DataBean addressBean;
        public String name;
        public String phone;
        public String address;
        public int addressId;



        public MyAddressEvent(String name ,String phone,String address,int addId) {
            this.name = name;
            this.phone = phone;
            this.address = address;
            this.addressId=addId;

        }
        public MyAddressEvent(AddressListBean.DataBean addressBean) {
           this.addressBean=addressBean;
        }



        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public int getAddressId() {
            return addressId;
        }

        public void setAddressId(int addressId) {
            this.addressId = addressId;
        }

        public AddressListBean.DataBean getAddressBean() {
            return addressBean;
        }

        public void setAddressBean(AddressListBean.DataBean addressBean) {
            this.addressBean = addressBean;
        }
    }

}
